package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.SnowFlake;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysDogOrder;
import com.ruoyi.project.system.service.ISysDogOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 电子狗订单Controller
 *
 * @author ruoyi
 * @date 2020-08-04
 */
@Slf4j
@RestController
@RequestMapping("/system/order")
public class SysDogOrderController extends BaseController
{
    @Autowired
    private ISysDogOrderService sysDogOrderService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询电子狗订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDogOrder sysDogOrder)
    {
        startPage();
        //获取当前登录用户信息，入参查询

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        log.info("loginUser = "+loginUser.getUser().getPhonenumber());
        sysDogOrder.setUserId(Long.valueOf(loginUser.getUser().getPhonenumber()));
        List<SysDogOrder> sysDogOrders = new ArrayList<>();
        List<SysDogOrder> list = sysDogOrderService.selectSysDogOrderList(sysDogOrder);

        //查询显示狗是否分裂的业务逻辑，写在插入里面。一次插入多次查询。避免分裂逻辑在查询中多次执行

        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                SysDogOrder order = new SysDogOrder();
                BeanUtils.copyProperties(list.get(i), order);

                Long price = order.getDogNowPrice() + order.getDogNowPrice() * order.getDogRate() / 100;
                System.out.println("price = " + price);
                if (price < order.getDogPriceUp()) {
                    order.setDogIsFenlie(0L);
                } else {
                    order.setDogIsFenlie(1L);
                }
                sysDogOrders.add(order);
            }
        }

        return getDataTable(sysDogOrders);
    }

    /**
     * 导出电子狗订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "电子狗订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDogOrder sysDogOrder)
    {
        List<SysDogOrder> list = sysDogOrderService.selectSysDogOrderList(sysDogOrder);
        ExcelUtil<SysDogOrder> util = new ExcelUtil<SysDogOrder>(SysDogOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取电子狗订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(sysDogOrderService.selectSysDogOrderById(orderId));
    }

    /**
     * 新增电子狗订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "电子狗订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDogOrder sysDogOrder)
    {
        SnowFlake worker = new SnowFlake(1,1,1);
        sysDogOrder.setOrderNo(""+worker.nextId());
        return toAjax(sysDogOrderService.insertSysDogOrder(sysDogOrder));
    }

    /**
     * 修改电子狗订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "电子狗订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDogOrder sysDogOrder)
    {
        return toAjax(sysDogOrderService.updateSysDogOrder(sysDogOrder));
    }

    /**
     * 删除电子狗订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "电子狗订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        System.out.println(""+orderIds);
        return toAjax(sysDogOrderService.deleteSysDogOrderByIds(orderIds));
    }
}
